package abd.pr1.Controllers;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.sql.DataSource;
import javax.swing.ImageIcon;

import abd.pr1.Interface.AdminUserPanel;
import abd.pr1.Interface.UserPanel;
import abd.pr1.Models.Usuario;

public class UserPanelController implements ActionListener,MouseListener {
	
	private MainWindowController mainWindowController;
	private UserPanel userPanel;
	private DataSource ds;
	private Usuario user;
	
	public UserPanelController(MainWindowController mainWindowController,UserPanel userPanel,DataSource ds, Usuario user){
		
		this.userPanel= userPanel;
		this.ds=ds;
		this.user=user;
		this.mainWindowController = mainWindowController;
		
		this.userPanel.addListener(this);
		
		refreshUserData();

	}

	
	public void refreshUserData(){
		this.userPanel.setNick(user.getNick());
		
		if(user.getNacimiento()!=null){
			this.userPanel.setEdad(calcularEdad(user.getNacimiento())+" años");
		} else {
			this.userPanel.setEdad("fecha de nacimiento no introducida");
		}
		this.userPanel.setImagen(new ImageIcon(user.getImagen()));
		
	}
	public String calcularEdad(Date fecha) {
		Calendar cal=Calendar.getInstance();
		cal.setTime(fecha);
		Calendar hoy = Calendar.getInstance();
		Integer edad = new Integer(hoy.get(Calendar.YEAR) - cal.get(Calendar.YEAR));
		if(hoy.get(Calendar.DAY_OF_YEAR)<=cal.get(Calendar.DAY_OF_YEAR)){
			edad--;
		}
		
		return edad.toString();
       

}
	@Override
	public void actionPerformed(ActionEvent e) {
		
		
	}


	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		new AdminUserController(this, new AdminUserPanel(), ds, user);
	}


	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
}
